package com.yungu.swift.api.route.v1.driver;


import com.alibaba.dubbo.config.annotation.Reference;
import com.yungu.swift.api.base.BaseApi;
import com.yungu.swift.api.facade.DriverFacade;
import com.yungu.swift.assets.driver.service.DriverService;
import com.yungu.swift.base.ResponseData;
import com.yungu.swift.constants.CommonConstant;
import com.yungu.swift.order.service.OrderFareService;
import com.yungu.swift.order.service.OrderService;
import com.yungu.swift.order.service.PayService;
import com.yungu.swift.utils.MapUtils;
import com.yungu.swift.utils.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * 描述：
 *
 * @author Zerrion
 * @Time 2018/2/28
 */
@Controller
@RequestMapping(value = "/api/v1/driver")
@Api(value = "【司机】【订单】【出租车】订单接口", tags = "出租车司机订单相关操作")
public class DriTaxiOrderApi extends BaseApi {

    @Autowired
    private DriverFacade driverFacade;

    @Reference
    private DriverService driverService;
    @Reference
    private OrderService orderService;
    @Reference
    private OrderFareService orderFareService;
    @Reference
    private PayService payService;


    @RequestMapping(value = {"/token/order/arrive"}, method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    @ApiOperation(value = "司机到达目的地接口", notes = "到达目的地", response = ResponseData.class)
    public ResponseData<Boolean> arrive(
            @ApiParam(name = "orderUuid", value = "订单id", required = true) @RequestParam String orderUuid,
            @ApiParam(name = "tripDistance", value = "实际行驶里程(km)", required = true) @RequestParam Double tripDistance,
            @ApiParam(name = "destAddress", value = "终点地址标题") @RequestParam(required = false) String destAddress,
            @ApiParam(name = "destDetailAddress", value = "终点的详细地址") @RequestParam(required = false) String destDetailAddress,
            @ApiParam(name = "destCity", value = "终点城市") @RequestParam(required = false) String destCity,
            @ApiParam(name = "destLng", value = "终点经度") @RequestParam(required = false) Double destLng,
            @ApiParam(name = "destLat", value = "终点纬度") @RequestParam(required = false) Double destLat,
            @ApiParam(name = "uploadText", value = "长连接上传的报文") @RequestParam(required = false) String uploadText,
            HttpServletRequest request) {
        return driverFacade.arrive(getUserUid(request), orderUuid, tripDistance, CommonConstant.BUSINESS_TYPE_TAXI,
                destAddress, destDetailAddress, destCity, destLng, destLat, uploadText, null);
    }

    @RequestMapping(value = {"/token/order/status"}, method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    @ApiOperation(value = "出租车司机首页获取订单状态", notes = "订单状态", response = ResponseData.class)
    public ResponseData<Map<String, Object>> status(@ApiParam(name = "appid", value = "APP标识", required = true) @RequestParam String appid,
                                                    HttpServletRequest request) {
        return driverFacade.status(getUserUid(request), CommonConstant.BUSINESS_TYPE_TAXI);
    }


    @RequestMapping(value = {"/token/order/updFare"}, method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    @ApiOperation(value = "出租车司机保存订单金额接口", notes = "保存金额", response = ResponseData.class)
    public ResponseData<Boolean> updFare(@ApiParam(name = "appid", value = "APP标识", required = true) @RequestParam String appid,
                                         @ApiParam(name = "orderUuid", value = "订单id", required = true) @RequestParam String orderUuid,
                                         @ApiParam(name = "actualFare", value = "订单金额", required = true) @RequestParam Double actualFare,
                                         @ApiParam(name = "otherFare", value = "其他金额", required = false) @RequestParam(required = false) Double otherFare) {
        if (!StringUtils.isNotEmpty(orderUuid) || StringUtils.isNull(actualFare)) {
            return ResponseData.buildErrorResponse(ResponseData.ERROR_CODE, "参数错误");
        }
        return orderService.updOrderFare(orderUuid, actualFare, otherFare);
    }


    @RequestMapping(value = {"/token/order/taxi/home"}, method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    @ApiOperation(value = "出租车司机首页接口", notes = "首页信息", response = ResponseData.class)
    public ResponseData<Map<String, Object>> home(
            @ApiParam(name = "appid", value = "APP标识", required = true) @RequestParam String appid,
            HttpServletRequest request) {

        //获取用户的信息
        String driverUuid = getUserUid(request);
        Map<String, Object> map = MapUtils.build(2);
        map.put("uuid", driverUuid);
        if (driverService.get(map) == null) {
            return ResponseData.buildErrorResponse(ResponseData.ERROR_CODE, "获取用户信息错误");
        }
        if (StringUtils.isEmpty(driverUuid) || StringUtils.isEmpty(appid)) {
            return ResponseData.buildErrorResponse(ResponseData.ERROR_CODE, "参数不能为空");
        }
        return orderService.queryHomePage(driverUuid, appid);
    }


    @RequestMapping(value = {"/token/order/fareItems"}, method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    @ApiOperation(value = "出租车司机订单费用列表详情接口", notes = "费用详情", response = ResponseData.class)
    public ResponseData<Map<String, Object>> fareItems(
            @ApiParam(name = "appid", value = "APP标识", required = true) @RequestParam String appid,
            @ApiParam(name = "orderUuid", value = "订单uuid", required = true) @RequestParam String orderUuid,
            @ApiParam(name = "couponUuid", value = "优惠券uuid，没有更换优惠券时，该值放空") @RequestParam(required = false) String couponUuid,
            HttpServletRequest request) {
        if (driverService.get(getUserUid(request)) == null) {
            return ResponseData.buildErrorResponse(ResponseData.ERROR_CODE, "获取用户信息错误");
        }
        return orderFareService.getFareItems(orderUuid, couponUuid,2);
    }

    @RequestMapping(value = {"/token/order/receiveCash"}, method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    @ApiOperation(value = "收到现金接口", notes = "收到现金接口", response = ResponseData.class)
    public ResponseData<Boolean> receiveCash(@ApiParam(name = "appid", value = "APP标识", required = true) @RequestParam String appid,
                                             @ApiParam(value = "订单id", name = "orderUuid", required = true) @RequestParam String orderUuid,
                                             @ApiParam(name = "uploadText", value = "长连接上传的报文", required = false) @RequestParam(required = false) String uploadText,
                                             HttpServletRequest request) {
        return payService.receiveCash(orderUuid, uploadText);
    }
}
